package com.example.demo.exception;

import com.example.demo.response.Response;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice(basePackages = "com.example.demo.controller")
@Slf4j
public class GlobalExceptionHandler {

    /*
        2.1 html静态页面：在resources/public/error/ 下定义
        如添加404页面： resources/public/error/404.html页面，中文注意页面编码
        2.2 模板引擎页面：在templates/error/下定义
        如添加5xx页面： templates/error/5xx.ftl
        注：templates/error/ 这个的优先级比较 resources/public/error/高

     */

    @ExceptionHandler(value = Exception.class)
    public Response handler(Exception e) {
        Response r = new Response();
        if (e instanceof RuntimeException) {
            RuntimeException selectNoFindException = (RuntimeException) e;
            r.setStatus(-1);
            r.setMsg(selectNoFindException.getMessage());
        } else {
            r.setStatus(-1);
            r.setMsg("系统错误");
        }
        log.error(e.getMessage());
        return r;
    }

    @Resource
    private HttpServletRequest request;

    @Resource
    private HttpServletResponse response;

//    @ExceptionHandler(Exception.class)
//    public ModelAndView handleException(Exception ex) {
//
//        /*
//         * 如果是Ajax引发的Exception异常的话,就将错误的信息返回给前台
//         * 然后在前台的error回调方法中访问后台的error接口,从而跳转到systemError页面
//         * */
//        String requestedWithHeader = request.getHeader("X-Requested-With");
//        if ("XMLHttpRequest".equals(requestedWithHeader)) {
//
//            // HttpServletResponse对象是java中的对象
//            // response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
//
//            // HttpStatus对象是Spring中的对象,指定错误的状态码,保证jquery的ajax走error方法
//            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
//
//            MappingJackson2JsonView jsonView = new MappingJackson2JsonView();
//            jsonView.setExtractValueFromSingleKeyModel(true);
//            jsonView.setModelKey("jsonKey");
//
//            ModelAndView mv = new ModelAndView(jsonView);
//            mv.addObject("jsonKey", Map.of("exception", ex.getMessage()));
//            return mv;
//        }
//        /*
//         * 重定向到指定的Controller,然后在Controller中,跳转到systemError页面
//         * */
//        String redirectPath = request.getContextPath() + "/error/500";
//        RedirectView redirectView = new RedirectView(redirectPath);
//        return new ModelAndView(redirectView, Map.of("exception", ex.getMessage()));
//    }


}